/**
 * 
 */
package com.dhcc.cdbp.query;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author 谢进军
 *
 */
public class BasicSearchingTest {
	private static final String directoryPath = "src/main/resources/lucenedir";
	private static final String fieldName = "contents";
	private static final String queryString = "i服了u";
	@Before
	public void setUp() throws Exception {
	}
	@Test public void test() throws IOException {
		Directory dir = FSDirectory.open(Paths.get(directoryPath,new String[0]));
		IndexReader reader = DirectoryReader.open(dir);
		IndexSearcher searcher = new IndexSearcher(reader);
		
		Term t = new Term(fieldName,queryString);
		Query query = new TermQuery(t);
		TopDocs docs = searcher.search(query, 10);
		assertEquals("Ant in Action", 1,docs.totalHits);
//		t = new Term("subject","junit");
//		docs = searcher.search(new TermQuery(t), 10);
//		assertEquals("Ant in Action,Junit in Action,Second Edition", 2,docs.totalHits);
		reader.close();
		dir.close();
	}
	@After
	public void tearDown() throws Exception {
	}
}
